package com.lin.chapter8;

import io.netty.bootstrap.Bootstrap;
import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.SimpleChannelInboundHandler;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioSocketChannel;
import io.netty.util.CharsetUtil;

import java.net.InetSocketAddress;

public class Client {
	public static void main(String[] args) throws InterruptedException {

		EventLoopGroup group = new NioEventLoopGroup();
		Bootstrap bootstrap = new Bootstrap();
		bootstrap.group(group)
				.channel(NioSocketChannel.class)
				.handler(new SimpleChannelInboundHandler<ByteBuf>() {
					@Override
					protected void channelRead0(ChannelHandlerContext channelHandlerContext, ByteBuf byteBuf) throws Exception {
						System.out.println("Recevied from Server: " + byteBuf.toString(CharsetUtil.UTF_8));
					}
				});
		ChannelFuture future = bootstrap.connect(new InetSocketAddress("localhost", 8080)).sync();
		future.addListener(channelFuture ->{
			if (channelFuture.isSuccess())
				System.out.println("Connect Success");
		});
	}
}
